

import 'dart:io';
import 'dart:typed_data';

import 'package:erp/Entity/RequestBuilderModel.dart';
import 'package:erp/Entity/ServerResponse.dart';
import 'package:erp/Entity/SignInResult.dart';
import 'package:erp/Events/EventAddrs.dart';
import 'package:erp/Store/AccountStore.dart';
import 'package:erp/global.dart';
import "dart:convert";
import 'package:dio/dio.dart';


void registerAccountEvent(){
  globalBus.subscribe<MapEntry<String, String>>(Events.ACCOUNT_SIGNIN_REQUEST, (bus, userNameAndPassword) async {
    try {
    await externalInterface.signIn(userNameAndPassword.key, userNameAndPassword.value, (ServerResponse<SignInResult> signInResult) {
        if (signInResult.type) {
          globalBus.publish(Events.ACCOUNT_SIGNIN_SUCCESS, signInResult);
          globalBus.publish(Events.FIRSTDATA_SELECT_SUCCESS, null);
        }
        else {
          globalBus.publish(Events.ACCOUNT_SIGNIN_ERROR, signInResult.msgText);
        }
      });
    }
    catch (exception){
      globalBus.publish(Events.ACCOUNT_SIGNIN_ERROR, exception);
    }
  });


  globalBus.subscribe<ServerResponse<SignInResult>>(Events.ACCOUNT_SIGNIN_SUCCESS, (bus, data){
    AccountStore.signInResult = data.msg;
  });

  globalBus.subscribe<ServerResponse<SignInResult>>(Events.ACCOUNT_SIGNIN_SUCCESS, (bus, data){
    globalPreferences.setString("userName", data.msg.phone);
  });
}

